package com.qianniu.napi.admin.service.akka;

import akka.actor.UntypedActor;
import com.alibaba.fastjson.JSON;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.IRedisService;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.*;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by chenxuebin on 2017/7/26.
 */
public class AKRealTemplate extends UntypedActor {

    private final static Logger logger = LoggerFactory.getLogger(AKRealTemplate.class);
//    private static final Log logLoseData = LogFactory.getLog("loseData");

    private IRedisService redis = (IRedisService) SpringUtil.getBean("redisServiceImpl");
//    private BaikeMapper baikeMapper = (BaikeMapper) SpringUtil.getBean("baikeMapper");
//
//    private WisdomMapper wisdomMapper = (WisdomMapper) SpringUtil.getBean("wisdomMapper");
//
//    private UserMapper userMapper = (UserMapper) SpringUtil.getBean("userMapper");

    @Override
    public void onReceive(Object message) {

        if (message instanceof String) {
            String msg = message.toString();
            logger.debug(">>>>>> AKRealTemplate receive message:" + msg);

            // 格式 type+uid+toke
            String[] arr = msg.split("@");
            if (arr.length < 3) return;

            String type = arr[0];
            String openid = arr[1];
            String mpid = arr[2];
            String exta = arr[3];

            switch (type) {
//                case Constants.WX_EVT_SIGNIN:
////                    createSignin(uid,wxType);
//                    PictureUtil.createSigninTemplate(WeixinUtil.getSignMap(uid),uid,false,wxType);
//                    break;
//                case Constants.WX_EVT_WISDOM:
//                    createWisdomMedia(uid,wxType, Boolean.valueOf(exta));
//                    break;
//                case Constants.WX_EVT_ENGLISH:
//                    createEnglishMedia(uid,wxType, Boolean.valueOf(exta));
//                    break;
                case Constants.WX_CHECK_USER:
                    WeixinUtil.checkUserInfo(openid, mpid, Boolean.valueOf(exta));
                    break;
                case Constants.WX_NIUNIUNIU:
                    logger.info("=====onReceive Constants.WX_NIUNIUNIU");
                    User user = redis.getUserByOpenid(openid);
                    PictureUtil.createNIUTemplate(user, Boolean.valueOf(exta));
                    break;
//                case Constants.WX_CREATE_PYQ:
//                    PictureUtil.createPYQTemplate(uid,exta);
//                    break;
//                case Constants.WX_CREATE_PYQPT:
//                    PictureUtil.createPYQPTTemplate(openid, exta);
//                    break;
                case Constants.WX_CREATE_QNB:
                    PictureUtil.createQNBTemplate(openid,mpid,exta);
                    break;
                case Constants.WX_CREATE_QNBPT:
                    PictureUtil.createQNBPTTemplate(openid,mpid,exta);
                    break;
                default:
                    return;
            }
        }
    }

    // 实时生成图片
    private void createWisdomMedia(String uid,String wxType,boolean isNotifyUser) {

//        logger.debug(">>>>>> createWisdomMedia start");
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("tdate", DateUtil.getToday());
//        map.put("tasktype", "wisdom");
//        TaskDO task = baikeMapper.selectTask(map);
//        logger.debug(">>>>>> createWisdomMedia task is " + JSON.toJSONString(task));
//        if (task == null || task.getId() <= 0) {
//            logger.debug(">>>>>> createWisdomMedia there is no wisdom task");
//            return;
//        }
//
//        String keyword = task.getContent();
//        map.put("tag", keyword);
//        List<WisdomDO> wisdoms = wisdomMapper.selectWisdom(map);
//        logger.debug(">>>>>> createWisdomMedia wisdoms.size()=" + wisdoms.size());
//        if (wisdoms == null || wisdoms.size() == 0) {
//            return;
//        }
//
//        String filepath = Constants.WX_QBD_IMAGEPATH_WISDOM + uid + "_" + DateUtil.getToday() + ".png";
//
//        File file = new File(filepath);
//
//        if (!file.exists()) {
//            logger.debug(">>>>>> createWisdom file not exist " + filepath);
//            logger.debug(">>>>>> start to createPersonWisdom file for uid=" + uid);
//
//            List<String> userlist = new ArrayList<String>();
//            userlist.add(uid);
//
//            logger.debug(">>>>>> start to prepareWisdomKeyword");
////            PictureUtil.prepareWisdomKeyword(wisdoms, keyword, userlist);
//
//            List<String> list = new ArrayList<String>();
//            int m = 1;
//            for (WisdomDO item :wisdoms) {
//                list.add(m + "、" + item.getContent());
//                m++;
//            }
//            PictureUtil.createWisdomTemplate(list,keyword,userlist,isNotifyUser);
//        }else{
//            logger.debug(">>>>>> createWisdom file exist " + filepath);
//        }
//
//        // 生成新图片
//
////            if (Constants.checkMap.get(isWisdomKey).compareAndSet(0, 1)
////                    && Strings.isNullOrEmpty(RedisUtils.getInstance().get(isWisdomKey))) {
////                RedisUtils.getInstance().set(isWisdomKey, Constants.REDIS_ONE);
////                String mediaId = WeixinUtil.uploadTempMaterial(token, Constants.WEIXIN_MATERIAL_IMAGE
////                        , uid, filepath, Constants.WX_EVT_WISDOM);
////                if(!Strings.isNullOrEmpty(mediaId)) {
////                    RedisUtils.getInstance().set(wisdomKey, mediaId);
////                    logger.debug(">>>>>> createWisdomMedia wisdomKey=" + wisdomKey + " mediaId=" + mediaId);
////                }else{
////                    logger.debug(">>>>>> createWisdomMedia failed for mediaId is null at wisdomKey=" + wisdomKey);
////                }
////            }
////        }
//
//        logger.debug(">>>>>> createWisdomMedia over");
    }

    private void createEnglishMedia(String uid,String wxType,boolean isNewUser) {
//
//        Map<String, String> map = new HashMap<String, String>();
//
////        List<String> uids = getUserlist(userId,isNewUser);
////        for (String uid :uids) {
//        String filepath = Constants.WX_QBD_IMAGEPATH_ENGLISH + uid + "_" + DateUtil.getToday() + ".png";
//        String englishKey = Constants.REDIS_KEYTYPE_ENGLISH + uid + "_" + DateUtil.getToday();
//        String isEnglishKey = Constants.REDIS_KEYTYPE_IS_ENGLISH + uid + "_" + DateUtil.getToday();
//        if (!Constants.checkMap.containsKey(isEnglishKey)) Constants.checkMap.put(isEnglishKey, new AtomicInteger(0));
//        Constants.checkMap.put(isEnglishKey, new AtomicInteger(0));
//        File file = new File(filepath);
//        if (file.exists()) {
//            if (Constants.checkMap.get(isEnglishKey).compareAndSet(0, 1)
//                    && RedisUtils.isNullOrEmpty(isEnglishKey)) {
//
//                RedisUtils.set(isEnglishKey,Constants.REDIS_ONE, 1, TimeUnit.DAYS);
//                String mediaId = WeixinUtil.uploadTempMaterial(wxType,
//                        Constants.WEIXIN_MATERIAL_IMAGE
//                        , uid, filepath, Constants.WX_EVT_ENGLISH,1);
//                RedisUtils.set(englishKey,mediaId, 1, TimeUnit.DAYS);
//
//                logger.debug(">>>>>> createEnglishMedia1 englishKey=" + englishKey + " mediaId=" + mediaId);
//            }
//        }
//        map.clear();
//        map.put("pdate", DateUtil.getToday());
//
//        List<EnglishDO> list = baikeMapper.selectEnglish(map);
//        EnglishDO english = null;
//        if (list == null || list.size() == 0) {
//            english = baikeMapper.selectLastEnglish(map);
//        } else {
//            english = list.get(0);
//        }
//
//        // 生成新图片
//        String imagefile = "/fafa/www" + english.getPath();
//        logger.debug(">>>>>> english=" + english.getEnglish() + " chinese=" + english.getChinese());
//        List<String> userlist = new ArrayList<String>();
//        userlist.add(uid);
//        String path = PictureUtil.createEnglishTemplate(imagefile, english.getEnglish(), english.getChinese(), userlist,isNewUser);

    }

}
